﻿using System;
using System.IO;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using eCitizen.DataContracts;
using eCitizen.eAlerts.Constants;

namespace eCitizen.eAlerts.Utilities
{
    public class ErrorLogManager
    {
        private SftpConfiguration _configuration;
        public void CreateErrorLogfile(SftpConfiguration currentServiceConfig,ServiceType currentServiceType, string errorMessage)
        {
            _configuration = currentServiceConfig;

            string errorFileName = FormErrorFileName(currentServiceType);
            string directory = Path.GetDirectoryName(errorFileName);
            if (!Directory.Exists(directory)) Directory.CreateDirectory(directory);
            TextWriter writer = new StreamWriter(errorFileName);

            writer.Write(errorMessage);

            writer.Close();

        }

        private string FormErrorFileName(ServiceType currentServiceType)
        {
            ServiceHelper currentServiceHelper = new ServiceHelper();
            string errorFileName = null;
            string errorFileFormat = AppKeyConstants.SendAlertErrorFileFormat;
            int runningFileNo = currentServiceHelper.GetRunningFileNo(currentServiceType, DateTime.Now);
            runningFileNo += 1;

            errorFileName = String.Format(errorFileFormat, DateTime.Now.ToString(ServiceConstants.DateFormats.YYYYMMDD), String.Format("{0:00000}",runningFileNo));

            errorFileName = _configuration.GetLocalOutboxFolder() + "\\" + errorFileName;
            return errorFileName;
        }

            }
}
